﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;

namespace ConsoleApp1
{
    public class StringName
    {
        private readonly string sqlCommandString = "select * from Table_1";

        private readonly string connectionString = "server=.;database=Xiaoli;uid=sa;pwd=123456";

        public static void Student()
        {
            string connectionString = "server=.;database=Xiaoli;uid=sa;pwd=123456";

            SqlConnection sqlConnection = new SqlConnection(connectionString);

            sqlConnection.Open();

            var sqlCommandString = "select * from Table_1";

            SqlCommand sqlCommand = new SqlCommand(sqlCommandString, sqlConnection);

            var sdr = sqlCommand.ExecuteReader();

            Console.WriteLine("{0}\t{1}\t{2}", "Id", "StudentName", "Sex");

            while (sdr.Read())
            {
                Console.WriteLine("{0}\t{1}\t{2}", sdr["Id"], sdr["StudentName"], sdr["Sex"]);
            }

            sqlConnection.Close();

        }
        public void PrintUserInfoAdaper()
        {
            SqlDataAdapter dataAdapter = new SqlDataAdapter(sqlCommandString,connectionString);

            DataTable dataTable = new DataTable();

            dataAdapter.Fill(dataTable);

            Console.WriteLine();

            Console.WriteLine();

            Console.WriteLine();

            Console.WriteLine("{0}\t{1}\t{2}", "Id", "StudentName", "Sex");

            for(int i = 0; i < dataTable.Rows.Count; i++)
            {
                Console.WriteLine("{0}\t{1}\t{2}", dataTable.Rows[i]["Id"], dataTable.Rows[i]["StudentName"],dataTable.Rows[i]["Sex"]);
            }
        }
    }
}
